package com.kkb.mybatis.framework.handler;

import com.kkb.mybatis.framework.config.BoundSql;
import com.kkb.mybatis.framework.config.ParameterMapping;

import java.sql.PreparedStatement;
import java.util.List;
import java.util.Map;

public class DefaultParameterHandler implements ParameterHandler {
    @Override
    public void setParameters(Object param, PreparedStatement preparedStatement, BoundSql boundSql) throws Exception{
        if (param instanceof Integer || param instanceof String) {
            preparedStatement.setObject(1, param);
        } else if (param instanceof Map) {
            Map<String, Object> map = (Map<String, Object>) param;
            List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
            for (int i = 0; i < parameterMappings.size(); i++) {
                ParameterMapping parameterMapping = parameterMappings.get(i);
                String name = parameterMapping.getName();
                Object value = map.get(name);
                preparedStatement.setObject(1 + i, value);
            }
        }
    }
}
